﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnterpriseServices.Service
{
    public static class Converters
    {
        //The only solution I can find is to iterate through the System.Array and 
        //populate the string[] object like so: 
        public static String[] ArrayToStringList(Array values)
        {
            // populate an Array object called 'values'  .... 
            // create a new string array 
            string[] newArray = new string[values.Length];

            // loop through the 2-D System.Array and populate the 1-D String Array 
            for (int i = 1; i <= values.Length; i++)
            {
                if (values.GetValue(1, i) == null)
                    newArray[i - 1] = "";
                else
                    newArray[i - 1] = (string)values.GetValue(1, i).ToString();
            }
            return newArray;
        }

        public static int[] ArrayToIntegerList(Array values)
        {
            // populate an Array object called 'values'  .... 
            // create a new Int array 
            int[] newArray = new int[values.Length];

            // loop through the 2-D System.Array and populate the 1-D String Array 
            for (int i = 1; i <= values.Length; i++)
            {
                newArray[i - 1] = (int)values.GetValue(1, i);
            }
            return newArray;
        }

        public static string[] StringCsvToStringList(string values)
        {
            // convert values to a String Array, split on the comma 
            string[] parms = values.Split(','); 
            return parms;
        }

    }

    public class CustomHttpBehavior : System.ServiceModel.Description.WebHttpBehavior
    {
        protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(System.ServiceModel.Description.OperationDescription operationDescription)
        {
            return new CustomQueryStringConverter();
        }
    }

    public class CustomQueryStringConverter : System.ServiceModel.Dispatcher.QueryStringConverter
    {
        public override bool CanConvert(Type type)
        {
            if (type == typeof(string[]))
            {
                return true;
            }
            return base.CanConvert(type);
        }
        public override object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType == typeof(string[]))
            {
                string[] parms = parameter.Split(','); return parms;
            }
            return base.ConvertStringToValue(parameter, parameterType);
        }
        public override string ConvertValueToString(object parameter, Type parameterType)
        {
            if (parameterType == typeof(string[]))
            {
                string valstring = string.Join(",", parameter as string[]); return valstring;
            }
            return base.ConvertValueToString(parameter, parameterType);
        }
    }

    public class CustomHttpBehaviorExtensionElement : System.ServiceModel.Configuration.BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            return new CustomHttpBehavior();
        }
        public override Type BehaviorType
        {
            get
            {
                return typeof(CustomHttpBehavior);
            }
        }
    }


}
